package sw6.digipecs.helpers;

import java.io.IOException;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;

public class AudioPlayer {
	private static AudioPlayer mInstance;
	private MediaPlayer mMediaPlayer = null; 
	
	private AudioPlayer(){}
	
	private static AudioPlayer getInstance()
	{
		if(mInstance==null)
			mInstance = new AudioPlayer(); 
		return mInstance; 
	}
	
	public static void open()
	{
		AudioPlayer ap = getInstance();
		ap.mMediaPlayer = new MediaPlayer();
	}
	
	public static void close()
	{
		AudioPlayer ap = getInstance();
		ap.mMediaPlayer.release();
	}
	
	public static void play(String path, final OnCompletionListener listener)
	{
		AudioPlayer ap = getInstance();
		 
		try {
			ap.mMediaPlayer.reset();
			ap.mMediaPlayer.setDataSource(path);
			ap.mMediaPlayer.prepare();
			if (listener != null)
				ap.mMediaPlayer.setOnCompletionListener(listener);
			ap.mMediaPlayer.start(); 
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
}
